<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 维修项管理
 * @author zxj
 *
 */
class Parts extends MY_Controller {
    private $t_part = 'maintain_part';
    function __construct() {
        parent::__construct();
        //加载模型
        $this->load->model('part_model');
        if($this->users['account'] != 'zlj'){
            redirect(blang_url('orders/order'));
        }
    }

    /**
     * 维修项列表
     */
    function index()
    {
        //获取表单数据
        $data = $formField = gp();
        /**
         * 搜索逻辑处理
         */
        $where = $whereLike = array();
        //获取表字段(即查询条件,时间查询额外处理)
        $fieldsList = $this->db->list_fields($this->t_part);
        //需要模糊查询的字段
        $likeAry = array('maintain_part_name');
        //拼接查询字符串
        $searchStr = 'products/parts/index/?';
        foreach ($formField as $key => $val) {
            if ($val==='' || !in_array($key, $fieldsList)) continue;
            if (in_array($key, $likeAry)) {
                $whereLike[$key] = $val;
            }else {
                $where[$key] = $val;
            }
            $searchStr .= $key . '=' . $val . '&';
        }
        $where['maintain_part_grade !='] = '0';
        $searchStr = rtrim($searchStr, '&');
        //分页
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url($searchStr);
        $config['total_rows'] = $this->part_model->countRows($this->t_part, $where, '', $whereLike);
        $config['per_page'] = 20;
        $config['uri_segment'] = config_item('enable_lang') ? 5 : 4;
        $page = max(1, intval($data['page']));
        $slimit = ($page - 1) * $config['per_page'];
        $this->pagination->initialize($config);
        $data['show_pages'] = $this->pagination->create_links();
        $num_page = ceil($config['total_rows'] / $config['per_page']);
        $data['show_params'] = "总共{$config['total_rows']}条记录   {$config['per_page']}条/页   当前第{$page}/{$num_page}页";
        //查询
        $datalist = $this->part_model->select($this->t_part, $slimit, $config['per_page'], '', $where, '', $whereLike);
        //重构数据
        foreach ($datalist as $key => &$vary) {
            $vary['maintain_part_name'] = MYLANG=='sc'? $vary['maintain_part_name'] : $vary['maintain_part_name_en'];
            $vary['id'] = $vary['maintain_part_id'];
        }
        unset($vary);
        $data['dataList'] = $datalist;

        //视图以及样式
        $this->load->view('defaults/parts_list', $data);
        $this->load->view('defaults/common/footer');
    }


    /**
     * 新增维修项
     */
    function add(){
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //插入数据
        $getData = gp();
        if ($getData['submits']) {
            $rsid = $this->part_model->addOrSetPart('',$getData);
            redirect(blang_url('products/parts/'));
            //js_goto(blang_url('users/member/'),'新增成功!');
        }else{
            $gradeList = array(
                '1'=>lang('part_yjwx'),
                '2'=>lang('part_ejwx'),
                '3'=>lang('part_sjwx')
            );
            $data['gradeList'] = $gradeList;
            //视图以及样式
            $this->load->view('defaults/parts_post',$data);
            $this->load->view('defaults/common/footer');
        }
    }

    /**
     * 修改维修项
     * @param string $id
     */
    function edit($id='')
    {
        //表单处理
        $data['post_type'] = __FUNCTION__;
        $id = empty($id)?gp('maintain_part_id'):intval($id);
        //插入数据
        $getData = gp();
        if ($getData['submits']) {
            $rsid = $this->part_model->addOrSetPart($id, $getData);
            redirect(blang_url('products/parts/'));
        } else {
            $partsFirst = $this->part_model->get_part($id);
            $data['partsFirst'] = $partsFirst;
            $gradeList = array(
                '1'=>lang('part_yjwx'),
                '2'=>lang('part_ejwx'),
                '3'=>lang('part_sjwx')
            );
            $data['gradeList'] = $gradeList;
            //默认选中
            $optGrade[$partsFirst['maintain_part_grade']] = 'selected';
            $data['optGrade'] = $optGrade;

            //视图以及样式
            $this->load->view('defaults/parts_post', $data);
            $this->load->view('defaults/common/footer');

        }
    }

    /**
     * 删除维修项
     * @param string $id
     */
   function del($id = ''){
       if(!$id) show_error('删除维修项失败');
       $this->part_model->delete($this->t_part,array('maintain_part_id'=>intval($id)));
   }


}
